package gk.example.adapter;

import gk.example.App;
import gk.example.util.T;
import gk.example.view.ProgressDialog;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import cn.bmob.v3.listener.UpdateListener;

import com.example.gkdate.R;

/**
 * 
 * Description:个人信息Adapter
 * 
 * @author Wenk.Chan（最新修改者）
 * @version 1.0（最新版本号）
 */
public class PersonalInfoAdapter extends BaseAdapter {
	private Context mContext;
	private FragmentManager mFragmentManager;

	public PersonalInfoAdapter(Context context, FragmentManager fragmentManager) {
		mFragmentManager = fragmentManager;
		mContext = context;
	}

	@Override
	public int getCount() {
		return 6;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		PersonalInfoViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new PersonalInfoViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_personal_info, parent, false);
			viewHolder.title = (TextView) convertView
					.findViewById(R.id.tv_personal_info_title);
			viewHolder.detail = (TextView) convertView
					.findViewById(R.id.tv_personal_info_detail);
			viewHolder.cbSex = (CheckBox) convertView
					.findViewById(R.id.cb_personal_info_sex);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (PersonalInfoViewHolder) convertView.getTag();
		}
		switch (position) {
		case 0:
			viewHolder.title.setText(mContext.getResources().getString(
					R.string.account));
			if (App.BmobUser.getUsername() != null) {
				viewHolder.detail.setText(App.BmobUser.getUsername());
			}

			break;
		case 1:
			viewHolder.title.setText(mContext.getResources().getString(
					R.string.nickname));
			if (App.BmobUser.getNickName() != null) {
				viewHolder.detail.setText(App.BmobUser.getNickName());
			}

			break;
		case 2:
			viewHolder.cbSex.setVisibility(View.VISIBLE);
			viewHolder.detail.setVisibility(View.INVISIBLE);
			viewHolder.title.setText(mContext.getResources().getString(
					R.string.sex));
			if (App.BmobUser.getSex() != null) {
				if (App.BmobUser.getSex().equals("男")) {
					viewHolder.cbSex.setChecked(true);
				} else {
					viewHolder.cbSex.setChecked(false);
				}
			}
			viewHolder.cbSex
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							final ProgressDialog progressDialog = new ProgressDialog();
							progressDialog.show(mFragmentManager, "loading");
							String sex = null;
							if (isChecked) {
								sex = "男";
							} else {
								sex = "女";
							}
							App.BmobUser.setSex(sex);
							App.BmobUser.update(mContext, new UpdateListener() {

								@Override
								public void onSuccess() {
									T.showShort(mContext, "修改成功");
									progressDialog.dismiss();
								}

								@Override
								public void onFailure(int arg0, String arg1) {
									if (App.BmobUser.getSex().equals("女")) {
										App.BmobUser.setSex("男");
									} else {
										App.BmobUser.setSex("女");
									}
									T.showShort(mContext, "修改失败");
									progressDialog.dismiss();
								}
							});

						}
					});
			break;
		case 3:
			viewHolder.title.setText(mContext.getResources().getString(
					R.string.contact));
			if (App.BmobUser.getContact() != null) {
				viewHolder.detail.setText(App.BmobUser.getContact());
			}
			break;

		case 4:
			viewHolder.title.setText(mContext.getResources().getString(
					R.string.Email));
			if (App.BmobUser.getEmail() == null) {
				viewHolder.detail.setText(mContext.getResources().getString(
						R.string.press_to_change));
			} else {
				viewHolder.detail.setText(App.BmobUser.getEmail());
			}
			break;
		case 5:
			viewHolder.title.setText(mContext.getResources().getString(
					R.string.personal_introduce));
			if (App.BmobUser.getIntroduction() == null) {
				viewHolder.detail.setText(mContext.getResources().getString(
						R.string.press_to_change));
			} else {
				viewHolder.detail.setText(App.BmobUser.getIntroduction());
			}
			break;
		default:
			break;
		}
		return convertView;
	}
}

class PersonalInfoViewHolder {
	TextView title;
	TextView detail;
	CheckBox cbSex;
}
